<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class CouponController extends AdminController
{

    /**
     * 行为日志列表
     * @author huajie <banhuajie@163.com>
     */

    public function index()
    {
        $where = array();
        $list = $this->lists('Coupon', $where, "", true);

       foreach($list as $key=>$value){
           if($value["full_type"] == '1'){
                $list[$key]['full_type'] = "满减";
            }elseif($value["full_type"] == '2'){
                $list[$key]['full_type'] = "折扣";
            }else{
                $list[$key]['full_type'] = "现金劵";
           }
        }
        int_to_string($list);
        $this->assign('_list', $list);
        $this->display();
    }

    public function order()
     {
         $where = array();
         $id = $_GET["id"];
         $couponinfo['full_type'] = '1';
         $this->assign("couponinfo",$couponinfo);

         if($id){
             $where['id'] = $id;
             $couponinfo = M("Coupon")->where($where)->find();
             $this->assign("couponinfo",$couponinfo);
         }

         $this->display();

     }



//*******************************************************************
    public function addCoupon()
    {
        $data = array();
        $data['full_type'] = $_POST['full_type'];
        $data['title'] = $_POST['title'];
        $data['collection_time'] = $_POST['collection_time'];
        $data['denomination'] = $_POST['denomination'];
        $data['discount_rate'] = $_POST['discount_rate'];
        $data['condition'] = $_POST['condition'];
        $data['range'] = $_POST['range'];
        $data['term_of_validity'] = $_POST['term_of_validity'];
        $data['start_time'] = $_POST['start_time'];
        $data['end_time'] = $_POST['end_time'];
        $data['circulation'] = $_POST['circulation'];
        $data['user_identity'] = $_POST['user_identity'];
        $data['collar_setting'] = $_POST['collar_setting'];
        $data['received_circulation'] = $_POST['received_circulation'];
        $data['release_time'] = $_POST['release_time'];
        $data['validity'] = $_POST['end_time'];
        //新增时间 就是当前时间
        $data['release_time'] = date("Y-m-d H:i:s");
        $circulation = $_POST['circulation'];
        //判断这个优惠卷原先的个数是否有变化


        if($_POST['id']){
            $id = $_POST['id'];
            $couponed = M("UserCoupon")->where(array("coupon_id"=>$id))->select();
            $couponcount = count($couponed);
            if($circulation<$couponcount){
                $this->error('优惠卷的数量已经发出不能减少');
            }
            $circulation = $circulation-$couponcount;
            M("Coupon")->where(array("id"=>$_POST['id']))->save($data);//保存数据
        }else{
            $id = M("Coupon")->add($data);//新增
        }
        //新增优惠卷
        for($i=0;$i<$circulation;$i++){
            $couponcode = date("Ymd").rand(100000,999999);
            $data = array();
            $data['coupon_id'] = $id;
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['express_time'] = $_POST['end_time'];
            $data['status'] = "1";
            $data['order_no'] = "";
            $data['couponcode'] = $couponcode;
            M("UserCoupon")->add($data);//新增
        }
        //如果需要发送给用户直接发送
        if($_POST['user_id']){
            $couponed = M("UserCoupon")->where(array("coupon_id"=>$id,"user_id"=>"0"))->find();
            M("UserCoupon")->where(array("id"=>$couponed['id']))->save(array("user_id"=>$_POST['user_id']));//新增
        }
        $this->success('操作成功', U('Coupon/index'));//跳转到这个网页
    }
}


